using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;

public class BinaryHandle
{
    public static string ReadString(BinaryReader binary)
    {
        return binary.ReadString();
//         int len = binary.ReadInt16();
//         return new string(binary.ReadChars(len));
    }

    public static float ReadFloat(BinaryReader binary)
    {
        return binary.ReadSingle();
    }

    public static int ReadInt(BinaryReader binary)
    {
        return binary.ReadInt32();
    }

    public static uint ReadUInt(BinaryReader binary)
    {
        return binary.ReadUInt32();
    }

    public static sbyte ReadByte(BinaryReader binary)
    {
        return binary.ReadSByte();
    }

    public static byte ReadUByte(BinaryReader binary)
    {
        return binary.ReadByte();
    }

    public static short ReadShort(BinaryReader binary)
    {
        return binary.ReadInt16();
    }

    public static ushort ReadUShort(BinaryReader binary)
    {
        return binary.ReadUInt16();
    }

    public static void WriteString(BinaryWriter binary, string s)
    {
//         ushort len = (ushort)s.Length;
//         binary.Write(len);
//         binary.Write(ToByte(s.ToCharArray()));
        binary.Write(s);
    }

    public static void WriteFloat(BinaryWriter binary, float v)
    {
        binary.Write(v);
    }

    public static void WriteInt(BinaryWriter binary, int v)
    {
        binary.Write(v);
    }

    public static void WriteUInt(BinaryWriter binary, uint v)
    {
        binary.Write(v);
    }

    public static void WriteByte(BinaryWriter binary, sbyte v)
    {
        binary.Write(v);
    }

    public static void WriteUByte(BinaryWriter binary, byte v)
    {
        binary.Write(v);
    }

    public static void WriteShort(BinaryWriter binary, short v)
    {
        binary.Write(v);
    }

    public static void WriteUShort(BinaryWriter binary, ushort v)
    {
        binary.Write(v);
    }

    static public void Write7BitEncodedInt(BinaryWriter binary, int value)
    {
        uint v = (uint)value;
        while ((v & 0xffffff80) != 0)
        {
            byte b = (byte)((v & 0xff) | 0x80);
            binary.Write(b);
            v = v >> 7;
        }

        binary.Write((byte)v);
    }

    static public int Read7BitEncodedInt(BinaryReader binary)
    {
        int value = 0;
        byte b = binary.ReadByte();
        int index = 0;
        while ((b & 0x80) != 0)
        {
            b = (byte)(b & 0x7f);
            value |= (b << (index * 7));
            b = binary.ReadByte();
            index++;
        }

        return (int)value | (b << index * 7);
    }
}